Prueba Practica

Se buscar encontrar la eficiencia de la generación de números pseudo-aletorios a través de los métodos de cuadrados medios y congruencia lineal, para ello se debe seguir el siguiente proceso:

  1. A traves de la misma api generar una semilla diferente.

  2. Encontrar el numero de iteraciones hasta que se repita uno de sus datos.

  3. Generar 100 simulaciones con diferentes semillas.

  4. Generar un histograma con el resultado obtenidos por cada mƩtodo.

  5. Agregar sus conclusiones, opiniones y recomendaciones

Se debe generar un cuaderno de python para la simulación y subir dentro de este apartado.

Nota* para obtener la temperatura del CPU Descargarse OpenHardwareMonitor link https://openhardwaremonitor.org/downloads/ Abra el Monitor de hardware y ejecute el cuaderno

Metodo Cuadrados Medios

In [6]:
import numpy as np
import matplotlib.pyplot as plt
from tabulate import tabulate
from math import floor
import plotly.graph_objects as go
import wmi
import re
ui22=[]
similar=[]
nn=101
repetidos={}
for ii in range(1,101):
    
    ##se recomienda definir la semilla con digitos x0>3
    w = wmi.WMI(namespace="root\OpenHardwareMonitor")
    t = w.Sensor()
    x0= int(re.sub('[\.-]','',str(t[1].Value)[0:5]))
    print('Simulacion numero=: ',ii,'Con Semilla=: ',x0)
    
    ## NĆŗmero de digitos
    digitos= 4
    ##x0 elevado a la potencia 2
    xn=pow(int(x0), 2)
    a=len(str(xn))
    ban=False
    ite=[]
    xn2=[]
    xi=[]
    ui2=[]
    rn2=[]
    
    l=[]
    
    for i in range(1,nn):
        ite.append(i)
        ## Elevamos al cuadrado la semilla
        
        xi.append(x0)
        x02= pow(int(x0), 2)
        ##Sacamos la longitud del valor del cuadrado de la semilla
        longitud= len(str(x02))
        ##Determinamos si la longitud es la misma que la inicial 
        #Si no es usamos el metodo de aƱadir "0" al lado izquierdo del numero
        if longitud<a:
            x02 = str(x02).zfill(a)
            longitud= len(str(x02))
        ## Calculamos UI en base a los digitos
        xn2.append(x02)
        l.append(longitud)
        cant= str(x02)
        aux= floor(len(cant)/2)
        di=digitos/2
        inicio=int(aux)-int(di)
        fin=int(aux)+int(di)
        cantt= str(x02)
        if digitos%2==0:
            ui=cantt[inicio:fin]
        else:
            ui=cantt[inicio:fin+1]
        if(ban==True):
            ui2.append(ui)
            ui2.remove(ui)
            l.remove(longitud)
            xn2.remove(x02)
            xi.remove(x0)
            ite.remove(i)
            break
        elif(ban==False):
            ui2.append(ui)
            if(i==nn):
                n=+10

        for aa in ui2:
            if(ui2.count(aa)>1):
                ui22.append(i)
                similar.append(aa)
                repetidos[i]=aa
                ban=True
                break

        dd='1'
        dig=dd.ljust(digitos+1,'0')
        Rn=int(ui)/int(dig)
        rn2.append(Rn)
        x0=ui



    ##generamos una tabla con la libreria plotly.graph_objects
    fig = go.Figure(data=[go.Table(header=dict(values=['Iteración','xn', 'xn*xn','Logintud','ui','rn']),cells=dict(values=[ite,xi, xn2, l, ui2, rn2]))])
    fig.show()
Simulacion numero=:  1 Con Semilla=:  6848
Simulacion numero=:  2 Con Semilla=:  6083
Simulacion numero=:  3 Con Semilla=:  9043
Simulacion numero=:  4 Con Semilla=:  1059
Simulacion numero=:  5 Con Semilla=:  9056
Simulacion numero=:  6 Con Semilla=:  9807
Simulacion numero=:  7 Con Semilla=:  9912
Simulacion numero=:  8 Con Semilla=:  9452
Simulacion numero=:  9 Con Semilla=:  1013
Simulacion numero=:  10 Con Semilla=:  1060
Simulacion numero=:  11 Con Semilla=:  9401
Simulacion numero=:  12 Con Semilla=:  9410
Simulacion numero=:  13 Con Semilla=:  9610
Simulacion numero=:  14 Con Semilla=:  9641
Simulacion numero=:  15 Con Semilla=:  8856
Simulacion numero=:  16 Con Semilla=:  8390
Simulacion numero=:  17 Con Semilla=:  7775
Simulacion numero=:  18 Con Semilla=:  7065
Simulacion numero=:  19 Con Semilla=:  8873
Simulacion numero=:  20 Con Semilla=:  9041
Simulacion numero=:  21 Con Semilla=:  8705
Simulacion numero=:  22 Con Semilla=:  1092
Simulacion numero=:  23 Con Semilla=:  9154
Simulacion numero=:  24 Con Semilla=:  7418
Simulacion numero=:  25 Con Semilla=:  7387
Simulacion numero=:  26 Con Semilla=:  7447
Simulacion numero=:  27 Con Semilla=:  6916
Simulacion numero=:  28 Con Semilla=:  7437
Simulacion numero=:  29 Con Semilla=:  9177
Simulacion numero=:  30 Con Semilla=:  8832
Simulacion numero=:  31 Con Semilla=:  1148
Simulacion numero=:  32 Con Semilla=:  1195
Simulacion numero=:  33 Con Semilla=:  7633
Simulacion numero=:  34 Con Semilla=:  7848
Simulacion numero=:  35 Con Semilla=:  8618
Simulacion numero=:  36 Con Semilla=:  7852
Simulacion numero=:  37 Con Semilla=:  8846
Simulacion numero=:  38 Con Semilla=:  1128
Simulacion numero=:  39 Con Semilla=:  9310
Simulacion numero=:  40 Con Semilla=:  8794
Simulacion numero=:  41 Con Semilla=:  1017
Simulacion numero=:  42 Con Semilla=:  8926
Simulacion numero=:  43 Con Semilla=:  7980
Simulacion numero=:  44 Con Semilla=:  9528
Simulacion numero=:  45 Con Semilla=:  9132
Simulacion numero=:  46 Con Semilla=:  9213
Simulacion numero=:  47 Con Semilla=:  1032
Simulacion numero=:  48 Con Semilla=:  8039
Simulacion numero=:  49 Con Semilla=:  7275
Simulacion numero=:  50 Con Semilla=:  7169
Simulacion numero=:  51 Con Semilla=:  7951
Simulacion numero=:  52 Con Semilla=:  7118
Simulacion numero=:  53 Con Semilla=:  8591
Simulacion numero=:  54 Con Semilla=:  1026
Simulacion numero=:  55 Con Semilla=:  1080
Simulacion numero=:  56 Con Semilla=:  1087
Simulacion numero=:  57 Con Semilla=:  9662
Simulacion numero=:  58 Con Semilla=:  8514
Simulacion numero=:  59 Con Semilla=:  9445
Simulacion numero=:  60 Con Semilla=:  9184
Simulacion numero=:  61 Con Semilla=:  8540
Simulacion numero=:  62 Con Semilla=:  1047
Simulacion numero=:  63 Con Semilla=:  7597
Simulacion numero=:  64 Con Semilla=:  7739
Simulacion numero=:  65 Con Semilla=:  9184
Simulacion numero=:  66 Con Semilla=:  7459
Simulacion numero=:  67 Con Semilla=:  7717
Simulacion numero=:  68 Con Semilla=:  9486
Simulacion numero=:  69 Con Semilla=:  9314
Simulacion numero=:  70 Con Semilla=:  9441
Simulacion numero=:  71 Con Semilla=:  1058
Simulacion numero=:  72 Con Semilla=:  7424
Simulacion numero=:  73 Con Semilla=:  8111
Simulacion numero=:  74 Con Semilla=:  1022
Simulacion numero=:  75 Con Semilla=:  8515
Simulacion numero=:  76 Con Semilla=:  9162
Simulacion numero=:  77 Con Semilla=:  1107
Simulacion numero=:  78 Con Semilla=:  8560
Simulacion numero=:  79 Con Semilla=:  7402
Simulacion numero=:  80 Con Semilla=:  9636
Simulacion numero=:  81 Con Semilla=:  7920
Simulacion numero=:  82 Con Semilla=:  7865
Simulacion numero=:  83 Con Semilla=:  9621
Simulacion numero=:  84 Con Semilla=:  8402
Simulacion numero=:  85 Con Semilla=:  9474
Simulacion numero=:  86 Con Semilla=:  9926
Simulacion numero=:  87 Con Semilla=:  8033
Simulacion numero=:  88 Con Semilla=:  8824
Simulacion numero=:  89 Con Semilla=:  9516
Simulacion numero=:  90 Con Semilla=:  9835
Simulacion numero=:  91 Con Semilla=:  9243
Simulacion numero=:  92 Con Semilla=:  1036
Simulacion numero=:  93 Con Semilla=:  8320
Simulacion numero=:  94 Con Semilla=:  8986
Simulacion numero=:  95 Con Semilla=:  9038
Simulacion numero=:  96 Con Semilla=:  7484
Simulacion numero=:  97 Con Semilla=:  7297
Simulacion numero=:  98 Con Semilla=:  9724
Simulacion numero=:  99 Con Semilla=:  8660
Simulacion numero=:  100 Con Semilla=:  9886

Graficas Metodo Cuadrados Medios

In [11]:
plt.hist(similar, bins=10, edgecolor = 'black',linewidth=1)
plt.title("Numeros Repetidos")
plt.xlabel("Valores")
plt.ylabel("Cantidad")
plt.show()

plt.hist(ui22, bins=10, edgecolor = 'red',linewidth=1)
plt.title("Iteraciones Repetidas")
plt.xlabel("Iteraciones")
plt.ylabel("Cantidad")
plt.show()

Metodo Congruencia Lineal

In [13]:
sin=[]
repetidos1={}
uiu=[]
nn=150
for ii in range(1,101):
    
    a = 3
    b = 5
    m = 19
    ##Se genera la semilla mediante la temperatura del CPU
    w = wmi.WMI(namespace="root\OpenHardwareMonitor")
    t = w.Sensor()
    x0= int(re.sub('[\.-]','',str(t[1].Value)[0:5]))
    print('Simulacion numero=: ',ii,'Con Semilla=: ',x0)
    xn1=[]
    xnt1=[]
    unt1=[]
    un1=[]
    xn=x0
    un=" "
    itee=[]
    band=False
    
    for i in range(1,nn):
        itee.append(i)
        xn1.append(xn)
        if(band==True):
            un1.append(un)
            un1.remove(un)
            xn1.remove(xn)
            itee.remove(i)
            break
        elif(band==False):
            un1.append(un)
            if(i==nn):
                n=+10

        for aaa in un1:
            if(un1.count(aaa)>1):
                uiu.append(i)
                sin.append(aaa)
                repetidos1[i]=aaa
                band=True
                break

        xn=(a*xn+b)%m
        un=xn/m 


    fig = go.Figure(data=[go.Table(header=dict(values=["Iteraciones", "xn1","un1"]),
                     cells=dict(values=[itee, xn1, un1]))])
    fig.show()
    
Simulacion numero=:  1 Con Semilla=:  8934
Simulacion numero=:  2 Con Semilla=:  5881
Simulacion numero=:  3 Con Semilla=:  7062
Simulacion numero=:  4 Con Semilla=:  6856
Simulacion numero=:  5 Con Semilla=:  8019
Simulacion numero=:  6 Con Semilla=:  8212
Simulacion numero=:  7 Con Semilla=:  8602
Simulacion numero=:  8 Con Semilla=:  9611
Simulacion numero=:  9 Con Semilla=:  7746
Simulacion numero=:  10 Con Semilla=:  7773
Simulacion numero=:  11 Con Semilla=:  8574
Simulacion numero=:  12 Con Semilla=:  6945
Simulacion numero=:  13 Con Semilla=:  6701
Simulacion numero=:  14 Con Semilla=:  9104
Simulacion numero=:  15 Con Semilla=:  7938
Simulacion numero=:  16 Con Semilla=:  8192
Simulacion numero=:  17 Con Semilla=:  8279
Simulacion numero=:  18 Con Semilla=:  8318
Simulacion numero=:  19 Con Semilla=:  7781
Simulacion numero=:  20 Con Semilla=:  7479
Simulacion numero=:  21 Con Semilla=:  8484
Simulacion numero=:  22 Con Semilla=:  6822
Simulacion numero=:  23 Con Semilla=:  8308
Simulacion numero=:  24 Con Semilla=:  8851
Simulacion numero=:  25 Con Semilla=:  9222
Simulacion numero=:  26 Con Semilla=:  8234
Simulacion numero=:  27 Con Semilla=:  7277
Simulacion numero=:  28 Con Semilla=:  8518
Simulacion numero=:  29 Con Semilla=:  8879
Simulacion numero=:  30 Con Semilla=:  9386
Simulacion numero=:  31 Con Semilla=:  8389
Simulacion numero=:  32 Con Semilla=:  8532
Simulacion numero=:  33 Con Semilla=:  1025
Simulacion numero=:  34 Con Semilla=:  9581
Simulacion numero=:  35 Con Semilla=:  7952
Simulacion numero=:  36 Con Semilla=:  8893
Simulacion numero=:  37 Con Semilla=:  7395
Simulacion numero=:  38 Con Semilla=:  7130
Simulacion numero=:  39 Con Semilla=:  7315
Simulacion numero=:  40 Con Semilla=:  8514
Simulacion numero=:  41 Con Semilla=:  9399
Simulacion numero=:  42 Con Semilla=:  8363
Simulacion numero=:  43 Con Semilla=:  9095
Simulacion numero=:  44 Con Semilla=:  7227
Simulacion numero=:  45 Con Semilla=:  7207
Simulacion numero=:  46 Con Semilla=:  8603
Simulacion numero=:  47 Con Semilla=:  7455
Simulacion numero=:  48 Con Semilla=:  8065
Simulacion numero=:  49 Con Semilla=:  8271
Simulacion numero=:  50 Con Semilla=:  9160
Simulacion numero=:  51 Con Semilla=:  8083
Simulacion numero=:  52 Con Semilla=:  6841
Simulacion numero=:  53 Con Semilla=:  8438
Simulacion numero=:  54 Con Semilla=:  6872
Simulacion numero=:  55 Con Semilla=:  7163
Simulacion numero=:  56 Con Semilla=:  8414
Simulacion numero=:  57 Con Semilla=:  8462
Simulacion numero=:  58 Con Semilla=:  7913
Simulacion numero=:  59 Con Semilla=:  9590
Simulacion numero=:  60 Con Semilla=:  8220
Simulacion numero=:  61 Con Semilla=:  7131
Simulacion numero=:  62 Con Semilla=:  7999
Simulacion numero=:  63 Con Semilla=:  7585
Simulacion numero=:  64 Con Semilla=:  7295
Simulacion numero=:  65 Con Semilla=:  7128
Simulacion numero=:  66 Con Semilla=:  9043
Simulacion numero=:  67 Con Semilla=:  8041
Simulacion numero=:  68 Con Semilla=:  8757
Simulacion numero=:  69 Con Semilla=:  8539
Simulacion numero=:  70 Con Semilla=:  7431
Simulacion numero=:  71 Con Semilla=:  8116
Simulacion numero=:  72 Con Semilla=:  8749
Simulacion numero=:  73 Con Semilla=:  7413
Simulacion numero=:  74 Con Semilla=:  9172
Simulacion numero=:  75 Con Semilla=:  9226
Simulacion numero=:  76 Con Semilla=:  9986
Simulacion numero=:  77 Con Semilla=:  8920
Simulacion numero=:  78 Con Semilla=:  8656
Simulacion numero=:  79 Con Semilla=:  8005
Simulacion numero=:  80 Con Semilla=:  8393
Simulacion numero=:  81 Con Semilla=:  7862
Simulacion numero=:  82 Con Semilla=:  7949
Simulacion numero=:  83 Con Semilla=:  8945
Simulacion numero=:  84 Con Semilla=:  8938
Simulacion numero=:  85 Con Semilla=:  7061
Simulacion numero=:  86 Con Semilla=:  6879
Simulacion numero=:  87 Con Semilla=:  7205
Simulacion numero=:  88 Con Semilla=:  7937
Simulacion numero=:  89 Con Semilla=:  6938
Simulacion numero=:  90 Con Semilla=:  7569
Simulacion numero=:  91 Con Semilla=:  8526
Simulacion numero=:  92 Con Semilla=:  8286
Simulacion numero=:  93 Con Semilla=:  8955
Simulacion numero=:  94 Con Semilla=:  7822
Simulacion numero=:  95 Con Semilla=:  7909
Simulacion numero=:  96 Con Semilla=:  6715
Simulacion numero=:  97 Con Semilla=:  6858
Simulacion numero=:  98 Con Semilla=:  7578
Simulacion numero=:  99 Con Semilla=:  7009
Simulacion numero=:  100 Con Semilla=:  8502

Graficas Metodo Congruencia Lineal

In [14]:
plt.hist(sin, bins=10, edgecolor = 'black',linewidth=1)
plt.title("Numeros Repetidos")
plt.xlabel("Valores")
plt.ylabel("Cantidad")
plt.show()

plt.hist(uiu, bins=10, edgecolor = 'orange',linewidth=1)
plt.title("Iteraciones Repetidas")
plt.xlabel("Iteraciones")
plt.ylabel("Cantidad")
plt.show()

Recomendaciones y Conclusiones

Se recomienda establecer una semilla >3, y cada semilla de la simulacion de los 100 procesos debe presentar una semilla diferente esto se logra mediante la temperatura del CPU

Por lo tanto dependera mucho de los parametros iniciales para que por cada iteracion exista por lo menos una repetición, por este motivo creo que es mucho mejor hacer uso del metodo de congruencia lineal, independite de las variables este metodo es mas facil de implementar y los resultados son mejores proporcionando una mejor aleatoriedad que el metodo de cuadrados medios.

In [4]:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-4-2f48e79f98fd> in <module>
----> 1 from automagica import *
      2 get_number_of_cpu()

ModuleNotFoundError: No module named 'automagica'
In [ ]: